package com.crm.system.service.impl.internalTrainingManager;

import cn.hutool.core.util.ObjectUtil;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.model.entity.internalTrainingManager.NxFollow;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.internalTrainingManager.NxFollowQO;
import com.crm.model.vo.customerManager.CustomerVO;
import com.crm.model.vo.internalTrainingManager.NxFollowVO;
import com.crm.service.customerManager.CustomerDbService;
import com.crm.service.internalTrainingManager.NxFollowDbService;
import com.crm.system.service.internalTrainingManager.NxFollowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 内训联系人
 * @Author renshaorong
 * @Date 2021/9/14
 */
@Service
public class NxFollowServiceImpl implements NxFollowService {

    @Autowired
    NxFollowDbService nxFollowDbService;

    @Autowired
    private CustomerDbService customerDbService;

    @Override
    public boolean insertNxFollow(NxFollow nxFollow) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        nxFollow.setFollowId(CommonUtils.getGUID());
        nxFollow.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        nxFollow.setCreatePeopleId(userAccount.getAccountId());
        nxFollow.setCreateTm(new Date());
        return nxFollowDbService.insertNxFollow(nxFollow);
    }

    @Override
    public DhPageInfo<NxFollowVO> selectFollowRecordList(Integer pageIndex, Integer pageSize, NxFollowQO nxFollowQO, String sortName, String sortOrder) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        nxFollowQO.setCreatePeopleId(userAccount.getAccountId());

        //增加删除状态查询条件(0:正常;1:已删除)
        nxFollowQO.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());

        StringBuilder order = new StringBuilder();
        if (!StringUtils.isBlank(sortName) && !StringUtils.isBlank(sortOrder)) {
            switch (sortName) {
                case "createTm": {
                    order.append("t.create_tm ").append(sortOrder);
                }
                break;
                default:
                    break;
            }
        }

        Integer dateUnit = 0;
        if (!StringUtils.isBlank(nxFollowQO.getDateType())) {
            if ("thisDay".equals(nxFollowQO.getDateType())) {
                //今天
                dateUnit = 1;
            }
            if ("thisWeek".equals(nxFollowQO.getDateType())) {
                //本月
                dateUnit = 2;
            }
            if ("thisMonth".equals(nxFollowQO.getDateType())) {
                //本周
                dateUnit = 3;
            }
            if ("thisYear".equals(nxFollowQO.getDateType())) {
                //本年
                dateUnit = 4;
            }
        }
        List<NxFollowVO> nxFollowVOList = nxFollowDbService.selectFollowRecordList(nxFollowQO, order.toString(),dateUnit);
        PageInfo<NxFollowVO> pageInfo = new PageInfo<NxFollowVO>(nxFollowVOList);
        return new DhPageInfo<NxFollowVO>(pageInfo, nxFollowVOList);
    }

    /**
     * 线上跟进插入内训跟进记录表
     *
     * @param nxFollowQO
     * @Author renshaorong
     * @Date 2021/11/30
     */
    @Override
    public boolean onlineFollow(NxFollowQO nxFollowQO) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();//获取登录人信息
        if (nxFollowQO != null && nxFollowQO.getCallDuration() > 0) {//沟通时长大于0
            //封装跟进记录实体
            NxFollow nxFollow = new NxFollow();
            BeanUtils.copyProperties(nxFollowQO,nxFollow);
            nxFollow.setFollowId(CommonUtils.getGUID());//主键
            nxFollow.setFollowWay(1);//跟进方式1-线上跟进

            String customerId = nxFollow.getCustomerId();
            if(StringUtils.isNotEmpty(customerId)){//根据客户id查询客户名称
                CustomerVO customerVO = customerDbService.selectByCustomerId(customerId);
                if (ObjectUtil.isNotNull(customerVO)) {
                    nxFollow.setCustomerName(customerVO.getCustomerName());
                }
            }

            nxFollow.setCreatePeopleId(userAccount.getAccountId());//创建人ID
            nxFollow.setCreateTm(new Date());//创建时间
            nxFollow.setDeleteStatus((short) StatusCodeEnum.NORMAL_CODE.getState());
            //新增跟进记录
            return nxFollowDbService.insertNxFollow(nxFollow);
        }
        return false;
    }
}